﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using FarmacieDAL;

namespace Farmacie.Util
{
    public static class ArhivaGraphicEngine
    {
        public static List<Point> CalculeazaStocCritic(List<Arhiva> arhive, out double variantaEstimata)
        {
            List<Point> result = new List<Point>();
            variantaEstimata = 0;
            if (arhive.Count > 0)
            {
                var primaArhiva = arhive.OrderBy(x => x.Data).First();
                var ultimaArhiva = arhive.OrderBy(x => x.Data).Last();

                var varianta = arhive.OrderBy(x => x.Data).Skip(1).Sum(x => x.Cantitate) - arhive.OrderBy(x => x.Data).Take(arhive.Count - 1).Sum(x => x.Cantitate);

                if (arhive.Count > 1)
                {
                    varianta = (int)Math.Ceiling((varianta / (arhive.Count - 1)));
                }

                var startPoint = new Point() { X = primaArhiva.Week, Y = primaArhiva.Cantitate * primaArhiva.Medicament.Producator.Timp_Livrare };
                var finishPoint = new Point() { X = ultimaArhiva.Week, Y = arhive.Max(x => x.Cantitate) * primaArhiva.Medicament.Producator.Timp_Livrare + varianta };

                result.Add(startPoint);
                result.Add(finishPoint);

                variantaEstimata = varianta;
            }
            return result;
        }

        public static List<Point> CalculeazaArhivele(List<Arhiva> arhive)
        {
            List<Point> result = new List<Point>();

            foreach (Arhiva arhiva in arhive)
            {
                result.Add(new Point() { X = arhiva.Week, Y = arhiva.Cantitate });
            }

            return result;
        }
    }
}
